package inet.app.vod.view;

import inet.app.vod.activity.R;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.inet.app.framework.util.Log;
import com.inet.app.framework.util.hardware.ConvertUtil;

public class MyViewPagerHeader extends RelativeLayout {

	public static int HEIGHT_TAB = 40; // height tab is 40dp

	LinearLayout mContainerLayout;
	ViewPager mViewPager;

	BoldTextView[] mTabs;
	String[] mTitles;
	static Context mContext;
	static LinearLayout.LayoutParams mTabParams;
	static int mHeightTabPx = -1;
	static int mMargin = -1;
	static int mColorText = -1;
	static int mColorBackground = -1;
	static int mColorTextSelect = -1;
	static int mColorBackgroundSelect = -1;

	int mCurPosition = 0; // page current

	public MyViewPagerHeader(Context context) {
		super(context);
		initLayout(context, R.layout.view_myviewpager_header);
	}

	public MyViewPagerHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context, R.layout.view_myviewpager_header);
	}

	public MyViewPagerHeader(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initLayout(context, R.layout.view_myviewpager_header);
	}

	public void initLayout(Context context, int resId) {
		mContext = context;

		// initialize only once
		if (mHeightTabPx == -1) {
			Log.i("init params");
			mHeightTabPx = (int) ConvertUtil.convertDpToPixel(mContext,
					HEIGHT_TAB);
			mTabParams = new LinearLayout.LayoutParams(50, mHeightTabPx);
			mTabParams.weight = 1;
			mMargin = (int) ConvertUtil.convertDpToPixel(mContext, 2);
			mTabParams.setMargins(mMargin, mMargin, mMargin, mMargin);

			mColorTextSelect = mContext.getResources().getColor(
					R.color.white_text);
			mColorBackgroundSelect = mContext.getResources().getColor(
					R.color.light_gray);
			mColorText = mContext.getResources().getColor(R.color.gray_text);
			mColorBackground = mContext.getResources().getColor(
					R.color.dark_gray);
		}
		LayoutInflater layoutInflate = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflate.inflate(resId, this, true);
	}

	public void attachToViewPager(ViewPager viewPager, String[] titles,
			final ViewPager.OnPageChangeListener pageChangeListener) {
		if (viewPager.getAdapter().getCount() != titles.length) {
			throw new RuntimeException("number pages other number titles!");
		}
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				resetTabView(position);
				if (pageChangeListener != null) {
					pageChangeListener.onPageSelected(position);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrolled(arg0, arg1, arg2);
				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrollStateChanged(arg0);
				}
			}
		});

		mTitles = titles;
		mViewPager = viewPager;
		init(viewPager.getAdapter().getCount());
	}

	public void attachToViewPager(ViewPager viewPager, String[] titles) {
		attachToViewPager(viewPager, titles, null);
	}

	/**
	 * Init view for view page header
	 * 
	 * @param sizePage
	 *            number page of view pager
	 */
	private void init(int sizePage) {
		mContainerLayout = (LinearLayout) findViewById(R.id.container_layout);
		if (sizePage == 0) {
			return;
		}
		/* init tabs */
		mTabs = new BoldTextView[sizePage];
		for (int i = 0; i < sizePage; i++) {
			mTabs[i] = initTab(i, mTitles[i]);
			mTabs[i].setTag(i);
			if (i == 0) {
				setSelectButton(mTabs[i]);
			}
			mContainerLayout.addView(mTabs[i]);
		}
		mCurPosition = 0;
	}

	/**
	 * Reset tab view when change page
	 * 
	 * @param position
	 *            position of current page select
	 */
	private void resetTabView(int position) {
		BoldTextView currentSelectBtn = (BoldTextView) mContainerLayout
				.findViewWithTag(mCurPosition);
		BoldTextView targetSelectBtn = (BoldTextView) mContainerLayout
				.findViewWithTag(position);
		if (currentSelectBtn != null && targetSelectBtn != null) {
			// change current button select
			setNormalButton(currentSelectBtn);
			setSelectButton(targetSelectBtn);
			mCurPosition = position;
		}
	}

	/**
	 * Set button is normal
	 * 
	 * @param button
	 */
	private void setNormalButton(BoldTextView tab) {
		tab.setBackgroundColor(mColorBackground);
		tab.setTextColor(mColorText);
	}

	/**
	 * Set button is select
	 * 
	 * @param button
	 */
	private void setSelectButton(BoldTextView tab) {
		tab.setBackgroundColor(mColorBackgroundSelect);
		tab.setTextColor(mColorTextSelect);
	}

	private BoldTextView initTab(final int pos, String title) {
		BoldTextView tab = new BoldTextView(mContext);
		tab.setLayoutParams(mTabParams);
		setNormalButton(tab);
		tab.setText(title);
		tab.setGravity(Gravity.CENTER);
		tab.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mViewPager != null) {
					mViewPager.setCurrentItem(pos, true);
					mCurPosition = pos;
				}
			}
		});
		return tab;
	}
}
